package com.common.MultiColumnListView.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.starappallcommon.R;
/**
 * 列表底部加载更多的文件
 * 
 * <br>Created 2014-6-6 下午5:25:52
 * @version  
 * @author   cyw    		
 *
 * @see
 */
public class XListViewFooter extends LinearLayout {
	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_LOADING = 2;

	private Context mContext;

	private View mContentView;
	private View mProgressBar;
	private TextView mHintView;

	/**
	 * 
	 * Creates a new instance of XListViewFooter.
	 * <br>Created 2014-6-9 下午5:47:47
	 * @param context 上下文
	 */
	public XListViewFooter(Context context) {
		super(context);
		initView(context);
	}

	/**
	 * 
	 * Creates a new instance of XListViewFooter.
	 * <br>Created 2014-6-9 下午5:47:55
	 * @param context 上下文
	 * @param attrs 属性集
	 */
	public XListViewFooter(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	/**
	 * 设置状态
	 * 
	 * <br>Created 2014-6-9 下午5:48:07
	 * @param state 状态
	 * @author        :  cyw
	 */
	public void setState(int state) {
		mHintView.setVisibility(View.INVISIBLE);
		mProgressBar.setVisibility(View.INVISIBLE);
		mHintView.setVisibility(View.INVISIBLE);
		if (state == STATE_READY) {
			mHintView.setVisibility(View.VISIBLE);
			mHintView.setText(R.string.xlistview_footer_hint_ready);
		} else if (state == STATE_LOADING) {
			mProgressBar.setVisibility(View.VISIBLE);
		} else {
			mHintView.setVisibility(View.GONE);
			mHintView.setText(R.string.xlistview_footer_hint_normal);
		}
	}

	/**
	 * 设置底部margin
	 * 
	 * <br>Created 2014-6-9 下午5:48:22
	 * @param height 高度
	 * @author        :  cyw
	 */
	public void setBottomMargin(int height) {
		if (height < 0){
			return;
		}
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContentView.getLayoutParams();
		lp.bottomMargin = height;
		mContentView.setLayoutParams(lp);
	}

	/**
	 * 获取底部margin
	 * 
	 * <br>Created 2014-6-9 下午5:48:26
	 * @return int
	 * @author        :  cyw
	 */
	public int getBottomMargin() {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContentView.getLayoutParams();
		return lp.bottomMargin;
	}

	/**
	 * normal status
	 */
	public void normal() {
		mHintView.setVisibility(View.VISIBLE);
		mProgressBar.setVisibility(View.GONE);
	}
	
	/**
	 * normal end status
	 * 
	 * <br>Created 2014-6-9 下午5:49:16
	 * @author        :  cyw
	 */
	public void normalEnd() {
		mHintView.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.GONE);
	}

	/**
	 * loading status
	 */
	public void loading() {
		mHintView.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.VISIBLE);
	}

	/**
	 * hide footer when disable pull load more
	 */
	public void hide() {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContentView.getLayoutParams();
		lp.height = 0;
		mContentView.setLayoutParams(lp);
	}

	/**
	 * show footer
	 */
	public void show() {
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContentView.getLayoutParams();
		lp.height = LayoutParams.WRAP_CONTENT;
		mContentView.setLayoutParams(lp);
	}

	/**
	 * 初始化view
	 * 
	 * <br>Created 2014-6-9 下午5:49:59
	 * @param context 上下文
	 * @author        :  cyw
	 */
	private void initView(Context context) {
		mContext = context;
		LinearLayout moreView = (LinearLayout) LayoutInflater.from(mContext).
		        inflate(R.layout.wishwall_xlistview_footer, null);
		addView(moreView);
		moreView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		mContentView = moreView.findViewById(R.id.xlistview_footer_content);
		mProgressBar = moreView.findViewById(R.id.xlistview_footer_progressbar);
		mHintView = (TextView) moreView.findViewById(R.id.xlistview_footer_hint_textview);
	}

}
